﻿<?php
// this page includes the server side of application, exluding the admin part, sent to the client and displayed
error_reporting(0);

session_start();
if(!isset($_SESSION['user']))
{
 // TODO delete this
 if($_POST['do']=='Checkuser')
	{
		require_once ('./inc/database.php');
		require_once ('./inc/functions.php');
		require_once ('./config.php');
		require_once('./class/class.phpmailer.php');
		require_once('./class/class.smtp.php');
		
		db_connect();
	$username=mysql_escape_string($_POST['username']);
	$stringusername=sprintf("SELECT * FROM `user` WHERE `user_name`='$username'");
	$queryusername=mysql_query($stringusername) or die(mysql_error().' '.$stringusername);
	
	if(mysql_affected_rows()>0)
		{
			$row=mysql_fetch_assoc($queryusername);
			$pass=$row['user_password'];
			$email=$row['user_mail'];
			
			
			
			$mail = new PHPMailer();
			$mail->IsSMTP(); // use SMTP
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
			$mail->FromName="RestartIT CRM";
			$mail->From = "restartit@email.com"; // sender address
			$mail->AddAddress("$email"); // recipient address
			$mail->Subject = "שיחזור מערכת";// mail subject
			$mail->Body = "הסיסמא שלך המערכת היא :".$pass;
			$mail->WordWrap = 150;// allow 150 charechters per row
			$mail->CharSet = 'UTF-8';
			}
		else {echo '0'; }	

		}//end Checkuser
else 
	{
 		exit;
	}
}
if (!isset($_POST['do']))
	{
		exit;
	}
?>
<script src="inc/js/core.js" type="text/javascript"></script>
<script src="inc/js/jquery.timeago.js" type="text/javascript"></script>
<?php	
	
	require ('./inc/database.php');
	require ('./inc/functions.php');
	require ('./config.php');
	require_once('./class/class.phpmailer.php');
	require_once('./class/class.user.php');
	require_once('./class/class.project.php');
	require_once('./class/class.task.php');
	require_once('./class/class.project_access_details.php');
	require_once('./class/class.permissions.php');
	require_once('./class/class.comments.php');
	require_once('./class/class.dashboard.php');
	db_connect();
	
//returns all task in a given status id		
if($_POST['do']=='selector_status')
{
   $id_project=$_POST['id_project'];
   $id_status=$_POST['id'];
   $project= new project();
   $project->getby_id($id_project);
   $arr_task=$project->get_tasks_in_status($id_status); //returns all task from id_status as array
   	 if($arr_task!=0)
				 {
				 foreach($arr_task as $id_task)
				 	{   $task= new task();
					  	$task->getby_id($id_task);
						  {
							  $task->get_html_task(); //get task comments 
						  }
					}
				 }
}


if($_POST['do']=='selector_all_option_project')
{
 	 $id_customer=$_POST['id_customer'];
	 $status=$_POST['status'];
	 $from=$_POST['from'];
	 $to=$_POST['to'];	
	  {
		   $arr_project=project::get_project_in_option($id_customer,$status,$from,$to);
			 
   	       if($arr_project!=0)
			 {
				 ?><p class="admin_project_status">סטאטוס פרויקט: <?php echo project::get_status_project2($status); ?></p> <?
				  foreach($arr_project as $single_project)
				{ 		 
				     $tempProject= new project();
					 $tempProject->getby_id($single_project);
					 
					   ?>
					  
					   <?php
							  
							  ?> 
							   <a href="http:/mliberman-w7-lt/restartcrm/main.php?project=<? echo $tempProject->getid(); ?>">
							  <p class="admin_project_title">פרויקט: <?php echo  $tempProject->project_name ?></p> 
								</a>
							  <?php
							  //if($task->task_status_id!=1)
								{
									$tempProject->get_html_project();
								}
					  
					  }
			 }
		  else
			  {
			   echo "לא נמצאו פרויקטים.... ";	
			  }
		 
	  }
	 
	  
	
}

			



//send email to contact user
if($_POST['do']=='send_mail_contect')
{
 	  $id_task=$_POST['id_task'];
	  $content=$_POST['content'];
	  $user_id=$_POST['user_id'];
	  $com=new comments();
	  $com->user_id=$user_id;
	  $com->task_id=$id_task;
	  $com->comment_content=$content;
	  $task=new task();
	  $task->getby_id($id_task);
	  $name_project=project::get_name($task->project_id);
	  $name_user=user::get_name($user_id);
	  $project=new project();
	  $project->getby_id($task->project_id);
	  $user_contect=$project->get_contect_customer();
	  $number_task=$task->get_task_id();
	  if(!$user_contect==0)
	  {
		  $mail = new PHPMailer();
		  foreach($user_contect as $SingleUserContact)
		    {
				 if($SingleUserContact->send_mail==1)
				  {
					   $mail->AddAddress($SingleUserContact->user_mail);				   $haveAddress=true;
				  }
			}
			$mail->IsSMTP(); // use SMTP
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
			  $mail->CharSet="utf-8";
			  $mail->From = $mail_crm; // sender mail
			
			  $mail->FromName="RestartIT CRM";
			  $r="<table border='1px' style='border-collapse:collapse;' dir='rtl'  >
					 <tr><td> פרוייקט</td><td>$name_project</td></tr>
					  <tr><td> פנייה</td><td>$id_task</td></tr>
					 <tr><td> מאת</td><td>$name_user</td></tr>
					 <tr><td> תגובה</td><td>$content</td></tr>
				 </table>";
			  
			  $mail->Subject = "התקבלה תגובה חדשה  לפניה $number_task על ידי $name_user לפרוייקט $name_project  ";
			  $mail->Body = $r; 
			  $mail->IsHTML(true);
	  }
}

//returns project name according to id
if($_POST['do']=='get_name_project_for_add_task')
{

 echo $id=$_POST['id'];

 $project=new project();
 $project->getby_id($id);
 
echo  $project->project_name;
}

//check if user exist
if($_POST['do']=='isuser')
{
  $name_user=mysql_escape_string($_POST['name_user']);
  $user= new user();	
  $res=$user->isuser($name_user);
?><input id="hid_res" name="hid_res" type="hidden" value="<?php echo $res; ?>" /> <?php 
}

//profil settings
if($_POST['do']=='get_edit_profil')
{
  $user_id=$_POST['id'];
  $user=new user();
  $user->getby_id($user_id);
  ?>
     <form id="form_profil" class="admin_form" name="form_profil" method="post" >
      <h3>שינוי הגדרות פרופיל</h3>
      <fieldset>
      <div class="form_set">
      <label>שם משתמש</label>
      <input id="name_profil" name="name_profil" type="text" readonly="readonly" 
       value="<?php echo $user->user_name;?>" />
       </div>
       <div class="form_set">
      <label>סיסמא</label>
      <input id="pass_profil" name="pass_profil" type="password"  value="<?php echo $user->user_password;?> " />
      </div>
      <div class="form_set">
      <label>אימות סיסמא</label>
      <input id="pass_profil_2" name="pass_profil_2" type="password" class="profil" value="" />
      </div>
      <div class="form_set">
      <label>אימייל</label>
      <input id="mail_profil" name="mail_profil" type="text"  value="<?php echo $user->user_mail ?>"  />
      </div>
      <label>קבלת עדכונים על שינוי מצב פנייה</label>
      <input id="status_send_mail" name="status_send_mail" type="checkbox" value="1" <?php if($user->send_mail){?> checked="checked"<?php }?>  />
      </fieldset>
      
      <input id="send_profil" name="send_profil" type="submit"  value="עדכן"  />
      </form>
      <div id="error" ></div>
      <script>
       $(document).ready(function(e) {
		 $("#form_profil").validate ({ 
	        			 rules : { 
						           pass_profil: {required:true},
								   pass_profil_2:{required:true,equalTo:"#pass_profil"},
								   mail_profil:{required:true, email:true}  

 							     },
						messages : 
						          {
							     pass_profil:"סיסמא",
								 mail_profil:"מייל",
								  pass_profil_2:"אימות סיסמא"
								  },
								  errorLabelContainer:'#error',
	            			      errorElement: "li",
						 submitHandler: function(form) 
							 {  var id=<?php echo $user_id ?>;
							  if ($("#status_send_mail").attr('checked')=="checked")
							    {
									var stauts_mail=1;
								}
								else 
								 {
									 var stauts_mail=0;
								 }
								
							
							update_profil(id,$('#pass_profil').val(),$('#mail_profil').val(),stauts_mail);
							 }
					  });
    });
      
      </script>
<?php  	
}
//Update user profile after he pressed update
if($_POST['do']=='update_profil')
{
 $id_user=$_POST['id'];
 $pass=$_POST['pass'];
 $mail=$_POST['mail'];
 $status_send_mail=$_POST['status_send_mail'];
 $user=new user();
 $user->getby_id($id_user);
         $user->user_password=$pass;
		 $user->user_mail=$mail;	
		 $user->send_mail=$status_send_mail;
 echo $user->update();

}
//send mail to user when task postponed
if($_POST['do']=='send_mail_postponed')
{ 

  $text=$_POST['textarea'];
  $task=new task();
  $task->getby_id($_POST['task_from']);
  $user= new user();
  $user->getby_id($_POST['user_from']);
  $email=$user->user_mail;
  $str="בהמשך לשיחתנו פנייתך  :\n";
  $str.="'$task->task_content'  -נדחתה מכוון ש\n";
  $str.="$text";
  $number_task=$task->get_task_id();
  $name_project= project::get_name($task->project_id);
     $project = new project();
	 $project->getby_id($task->project_id);
	 $permi=$project->getby_project_id();
	  if($permi!=0)
	  {
		  $mail = new PHPMailer();
		  foreach($permi as $singlepermi)
		  {
			  $user_temp= new user;
			  $user_temp->getby_id($singlepermi->user_id);
			  
			  if($user_temp->send_mail==1)
			  {
				  $mail->AddAddress($user_temp->user_mail);
				  $haveAddress=true;
			  }
		  }
				 
			$mail->IsSMTP(); // use SMTP
			$mail->SMTPAuth   = true;                  // enable SMTP authentication
			$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
			$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
			$mail->Port       = 465;                   // set the SMTP port
			
			$mail->Username   = "do_not_reply@restartit.co.il";  // GMAIL username
			$mail->Password   = "restart4mail";            // GMAIL password
			
			//$mail->Host = "smtp.email.com"; // SMTP address
			$mail->From = "$mail_crm"; // sender mail
			
			$mail->Subject = " נדחתה פנייה $number_task לפרוייקט $name_project ";
			$mail->Body = $str;
			$mail->WordWrap = 150;// allow 150 charechters per row
			$mail->CharSet = 'UTF-8';
		    $mail->FromName="RestartIT CRM";
			//$mail->AddAttachment($temp,' תמונה לפני','base64','image/jpeg');
			if($haveAddress)
			 {	
				if($mail->Send()) // send mail
				{
				 echo " סיבת הדחייה נשלחה במייל";
				}
				else
				{
				  echo "לא ניתן לשלוח מייל";
				}
			 }
	 }
	
	
}

?>